package com.example.kugou.commons.file;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributeView;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class listTest {

    /**
     * 使用每行读取的方式
     * @param fileName
     */
    public  List<List<String>> getSingerInfo(String fileName) {
        List<List<String>> jianjie = new ArrayList<>();
        List<String> list = new ArrayList<String>();
        Map<String, Object> map = readFileByLines(fileName);

        List<Integer> integers = new ArrayList<>();
        integers = (List<Integer>) map.get("integers");
        list = (List<String>) map.get("list");
        int ftest = 0;//每次取的数据
        int kaishi = 0;
        List<String> subList = new ArrayList<>();
        if(integers.size() == 0){
            jianjie.add(list);
        }else {
            for (int i = 0; i < integers.size(); i++) {
                //设置开始的时候每段读取的初始值
                ftest = integers.get(i) - 1;
                subList = list.subList(kaishi, ftest);    //获取子列表
                jianjie.add(subList);
                kaishi = integers.get(i) - (i + 1);
                if (integers.size() - 1 == i) {
                    kaishi = ftest - i;
                    ftest = list.size() - 1;
                    subList = list.subList(kaishi, ftest);    //获取子列表
                    jianjie.add(subList);
                }
            }
        }
        return jianjie;
    }


    /**
     * 以行为单位读取文件，常用于读面向行的格式化文件
     */

    public  Map<String, Object> readFileByLines(String fileName) {

        File file = new File(fileName);
        List<Integer> integers = new ArrayList<>();
        BufferedReader reader = null;
        List<String> list = new ArrayList<String>();
        try {
//            System.out.println("以行为单位读取文件内容，一次读一整行：");
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            int line = 0;
//              一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                line++;
                //  显示行号
                if (tempString == null || tempString.equals("")) {
                    integers.add(line);
                } else {
                    list.add(tempString);
//                    System.out.println("line  " + line + ":  " + tempString);
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("integers", integers);
//        System.out.println(map);
        return map;
    }

    public static void main(String[] args) {
        List string= new listTest().getSingerInfo("E:\\小组项目\\网易云\\网易云\\163mHome\\singerInfo\\IU.txt");
        System.out.println(string);
//        try {
//            LocalDateTime localDateTime =  getCreateTime(new File("G:\\小组项目\\网易云\\歌曲\\歌手的介绍\\周兴哲\\周兴哲.txt"));
//            System.out.println(localDateTime);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

    }
    private static LocalDateTime getCreateTime(File file) throws IOException {
        Path path = Paths.get(file.getPath());
        BasicFileAttributeView basicfile = Files.getFileAttributeView(path, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
        BasicFileAttributes attr = basicfile.readAttributes();
        long date = attr.creationTime().toMillis();
        Instant instant = Instant.ofEpochMilli(date);
        return LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
    }


    /**
     * 读取文件创建时间
     */
    public static void getCreateTime(){
        String filePath = "G:\\小组项目\\网易云\\歌曲\\歌手的介绍\\周兴哲\\周兴哲.txt";
        String strTime = null;
        try {
            Process p = Runtime.getRuntime().exec("cmd /C dir "
                    + filePath
                    + "/tc" );
            InputStream is = p.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line;
            while((line = br.readLine()) != null){
                if(line.endsWith(".txt")){
                    strTime = line.substring(0,17);
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("创建时间 " + strTime);
        //输出:创建时间 2009-08-17 10:21
    }


    public static void  String(){
            File file = new File("G:\\小组项目\\网易云\\歌曲\\歌手的介绍\\周兴哲\\周兴哲.jpg");
            Path filePath = file.toPath();

            BasicFileAttributes attributes = null;
            try
            {
                attributes =
                        Files.readAttributes(filePath, BasicFileAttributes.class);
            }
            catch (IOException exception)
            {
                System.out.println("Exception handled when trying to get file " +
                        "attributes: " + exception.getMessage());
            }
            long milliseconds = attributes.creationTime().to(TimeUnit.MILLISECONDS);
            if((milliseconds > Long.MIN_VALUE) && (milliseconds < Long.MAX_VALUE))
            {
                Date creationDate =
                        new Date(attributes.creationTime().to(TimeUnit.MILLISECONDS));

                System.out.println("File " + filePath.toString() + " created " +
                        creationDate.getDate() + "/" +
                        (creationDate.getMonth() + 1) + "/" +
                        (creationDate.getYear() + 1900));
            }
    }

    /**
     * 读取文件修改时间的方法1
     */
    @SuppressWarnings("deprecation")
    public static void getModifiedTime_1(){
        File f = new File("G:\\小组项目\\网易云\\歌曲\\歌手的介绍\\周兴哲\\周兴哲.jpg");
        Calendar cal = Calendar.getInstance();
        long time = f.lastModified();
        cal.setTimeInMillis(time);
        //此处toLocalString()方法是不推荐的，但是仍可输出
        System.out.println("修改时间[1] " + cal.getTime().toLocaleString());
        //输出:修改时间[1] 2009-8-17 10:32:38
    }

}
